package com.richard.base.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.ViewCompat
import com.lcodecore.tkrefreshlayout.IHeaderView
import com.lcodecore.tkrefreshlayout.OnAnimEndListener
import com.richard.base.R
import com.richard.base.ext.dp2px

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/7/23     6:21 PM
 * 用途:
 ***************************************
 */
class RefreshHeader @JvmOverloads constructor(

    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr), IHeaderView {

    private val MAX_ALPHA = 255
    private lateinit var mCircleView: ImageView
    private lateinit var mTitleView: TextView
    private lateinit var animationDrawable: AnimationDrawable
    private var mCurrentDrawableName = ""

    init {
        createProgressView()
        ViewCompat.setChildrenDrawingOrderEnabled(this, true)
    }

    private fun createProgressView() {
        mCircleView = ImageView(context)
        mCircleView.scaleType = ImageView.ScaleType.FIT_XY
        mTitleView = TextView(context)
        mTitleView.textSize = 13f
        mTitleView.setTextColor(Color.parseColor("#9B9B9B"))
        mTitleView.visibility = View.GONE
        gravity = Gravity.CENTER
        orientation = VERTICAL
        val imgViewParams = LayoutParams(context.dp2px(20), context.dp2px(30))
        mCircleView.layoutParams = imgViewParams
        val txtViewParams = LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        txtViewParams.setMargins(0, context.dp2px(6), 0, 0)
        mTitleView.layoutParams = txtViewParams
        addView(mCircleView)
        addView(mTitleView)
    }

    /**
     * One of DEFAULT, or LARGE.
     */
    fun setSize(size: Int) {
        mCircleView.setImageDrawable(null)
        mCircleView.setImageResource(R.drawable.anim_loading_view)
    }

    override fun reset() {
        mCircleView.clearAnimation()
        mCircleView.visibility = View.GONE
        mCircleView.setImageDrawable(null)
    }

    fun getResource(imageName: String): Int {
        mCurrentDrawableName = imageName
        val ctx = context
        return resources.getIdentifier(imageName, "drawable", ctx.packageName)
    }

    override fun getView(): View? {
        return this
    }

    private var mIsBeingDragged = false

    override fun onPullingDown(
        fraction: Float,
        maxHeadHeight: Float,
        headHeight: Float
    ) {
        if (mCircleView.visibility != View.VISIBLE) {
            mCircleView.visibility = View.VISIBLE
        }
        setAnimResourse(fraction)
        if (!mIsBeingDragged) {
            mIsBeingDragged = true
        }
    }

    private fun setAnimResourse(fraction: Float) {
        var fraction = fraction
        if (fraction > 1) {
            fraction = 1f
        }
        val index = (fraction * 16).toInt() % 17
        val drawableName = String.format("comp_000%02d", index)
        if (mCurrentDrawableName != drawableName) {
            mCircleView.setImageResource(getResource(drawableName))
        }
    }

    override fun onPullReleasing(
        fraction: Float,
        maxHeadHeight: Float,
        headHeight: Float
    ) {
        mIsBeingDragged = false
    }

    override fun startAnim(maxHeadHeight: Float, headHeight: Float) {
        mCircleView.visibility = View.VISIBLE
        mCircleView.setImageDrawable(null)
        mCircleView.setImageResource(R.drawable.base_anim_refresh_loading)
        animationDrawable = mCircleView.drawable as AnimationDrawable
        animationDrawable.start()
    }

    override fun onFinish(animEndListener: OnAnimEndListener) {
        mCircleView!!.animate().scaleX(1f).scaleY(1f).alpha(1f)
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    reset()
                    animEndListener.onAnimEnd()
                }
            }).start()
    }

    /**
     * 设置下来刷新文案
     *
     * @param title
     */
    fun setTitle(title: String?) {
        if (mTitleView != null) {
            mTitleView.setVisibility(if (TextUtils.isEmpty(title)) View.GONE else View.VISIBLE)
            mTitleView.setText(title)
        }
    }
}